package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Panelchildren;

import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.acom.xhui.vimarket.zkplusst;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.AttrTmpl;
import com.xhui.vcom.xhui.vimarket.zkpluszul.attr.bean.ValiVo;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCcom.xhui.vimarket.zkplus/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrPanelComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrPanelComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrPanelComp inst   = new AttrPanelComp();
    
    /**
     * 日志.
     */
    private static Logger        logger = Logger.getLogger(AttrPanelComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrPanelComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrPanelComp instance() {
        return AttrPanelComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Panel panel = new Panel();
        panel.setWidth("100%");
        panel.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        panel.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        panel.setTitle(attrTmpl.getName());
        panel.setSclass("zp-panel");
        // panelchildren
        final Panelchildren panelchildren = new Panelchildren();
        // add to zul
        comp.appendChild(panel);
        panel.appendChild(panelchildren);
        // comp log
        if (AttrPanelComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<panel title=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" width=\"100%\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<panelchildren>");
        }
        
        // sub comp
        final int[] maxxy = AttrBaseComp.getMaxxy(attrExt, attrTmpl);
        final StringBuffer sbuf = new StringBuffer();
        if (maxxy[0] < 2) {
            sbuf.append("100%");
        } else {
            final int onew = 100 / maxxy[0];
            sbuf.append(onew);
            sbuf.append("%");
            for (int i = 1; i < maxxy[0]; i++) {
                sbuf.append(",");
                sbuf.append(onew);
                sbuf.append("%");
            }
        }
        AttrPanelComp.logger.debug(maxxy[0] + "-" + maxxy[1]);
        final int sum = attrTmpl.getAttrTmplListCount();
        int line = -1;
        // Hbox
        Hbox hbox = null;
        /*
         * = new Hbox(); hbox.setWidth("100%"); hbox.setWidths(sbuf.toString());
         * hbox.setParent(panelchildren);
         */
        AttrPanelComp.logger.debug("hbox");
        String spans = null; // 行宽度布局
        int rowSpansNum = -1; // 自动布局情况下，当行数量
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            if (AttrPanelComp.logger.isDebugEnabled()) {
                AttrPanelComp.logger.debug("x:" + subAttrTmpl.getXAddr() + ",y:"
                    + subAttrTmpl.getYAddr() + ",widths:" + sbuf.toString());
            }
            if ((AttrBaseComp.SORT_BY_XY == attrExt.getAttrAda().getSortType() && line != subAttrTmpl
                .getYAddr())
                || (AttrBaseComp.SORT_BY_ORDER == attrExt.getAttrAda().getSortType() && (rowSpansNum < 0 || rowSpansNum >= attrTmpl
                    .getXAddr()))) { // 新行，根据模式进行计算
                // if (line != -1) {
                hbox = new Hbox();
                hbox.setWidth("100%");
                hbox.setWidths(sbuf.toString());
                hbox.setParent(panelchildren);
                spans = "";
                rowSpansNum = 0;
                AttrPanelComp.logger.debug("hbox");
                // }
                // comp log
                if (AttrPanelComp.logger.isDebugEnabled()) {
                    if (line != -1) {
                        attrExt.appendInnerComp("</hbox>");
                    }
                    attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"");
                    attrExt.appendInnerComp(sbuf.toString());
                    attrExt.appendInnerComp("\">");
                }
                line = subAttrTmpl.getYAddr();
            }
            // 如果超出总长度，以实际长度设计
            int thisWidth = (subAttrTmpl == null || subAttrTmpl.getWidth() == 0) ? 1
                : subAttrTmpl.getWidth();
            if (rowSpansNum + thisWidth > attrTmpl.getXAddr()) {
                thisWidth = attrTmpl.getXAddr() - rowSpansNum;
                subAttrTmpl.setWidth(thisWidth);
            }
            spans = spans + ("".equals(spans) ? ""
                : ",") + thisWidth; // 行格分布拼接
            rowSpansNum += thisWidth;
            AttrCompFactory.getInst(subAttrTmpl.getDisplayMode())
                .create(attrExt, subAttrTmpl, hbox);
        }
        
        if (AttrPanelComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("</hbox>");
            attrExt.appendInnerComp("</panelchildren>");
            attrExt.appendInnerComp("</panel>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
        
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setValue(attrExt,
                        subCompRootBox, attrInstList, attrViewMap);
                }
            }
    com.xhui.vimarket.zkplus   
    /**
     * {@inheritDoc}
     * @see com.xhui.com.xhui.vimarket.d.AttrComp
     * #setRemovedValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setRemovedValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setRemovedValue(attrExt,
                        subCompRootBox, attrInstList);
                }
 com.xhui.vimarket.zkplus       }
    }
    
    /**
     * {@inheritDcom.xhui.vimarket.zkpluse com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setDisabledValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setDisabledValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setDisabledValue(attrExt,
                        subCompRootBox, attrInstList);
                }
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(
                        subCompRootBox).getValue(attrExt, subCompRootBox);
                    if (subRetList != null) {
                        retList.addAll(subRetList);
                    }
                }
        com.xhui.vimarket.zkplus}
        return retList;
    }
    
    com.xhui.vimarket.zkplusnheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<ValiVo> retList = new ArrayList<ValiVo>();
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    final ArrayList<ValiVo> subRetList = AttrCompFactory.getInstByComponent(
                        subCompRootBox).signRuleHint(attrExt, subCompRootBox);
                    if (subRetList != null) {
                        retList.addAll(subRetList);
                    }
                }
            }
        }
        return retList;
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(
                        subCompRootBox).getNullValue(attrExt, subCompRootBox);
                    if (subRetList != null) {
                        retList.addAll(subRetList);
                    }
                }
            }
        }
        return retList;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        for (final AttrTmpl subAttrTmpl : attrTmpl.getAttrTmplList()) {
            final ArrayList<AttrInst> subRetList = AttrCompFactory.getInst(
                subAttrTmpl.getDisplayMode()).getNullValue(attrExt, subAttrTmpl);
            //20120820 经宇星确认为NULL是不做下面逻辑
            if (subRetList != null) {
                retList.addAll(subRetList);
            }
        }
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).clean(attrExt,
                        subCompRootBox);
                }
            }
        }
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setDisabled(attrExt,
                        subCompRocom.xhui.vimarket.zkplus
                }
            }
        }
    com.xhui.vimarket.zkplus*
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp
     * #validate(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-3-22 wuyx
     */
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        final ArrayList<RetVO> retList = new ArrayList<RetVO>();
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    final List<RetVO> subRetList = AttrCompFactory.getInstByComponent(
                        subCompRootBox).validate(attrExt, subCompRootBox);
                    if (subRetList != null) {
                        retList.addAll(subRetList);
              com.xhui.vimarket.zkplus          }
            }
        }
        return retLicom.xhui.vimarket.zkplus 
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp
     * #refreshButtonText(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-3-24 wuyx
     */
    @Override
    public void refreshButtonText(AttrExt attrExt, Component comp, final List<AttrInst> attrInstList) {
        final ArrayList<RetVO> retList = new ArrayList<RetVO>();
        // 自身不实例化，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).refreshButtonText(attrExt,
                        subCompRootBox, attrInstList);
                }
            }
        }
    }
    
    /**
com.xhui.vimarket.zkplus.
     * 
     * @param comp 组件
     * @param attrViewMap 信息是否可见映照
     */
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(Component comp, Map<String, Integer> attrViewMap) {
        super.setAttrView(comp, attrViewMap);
        // 自身不隐藏，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setAttrView(subCompRootBox,
                        attrViewMap);
                }
            }
        }
    }
    
com.xhui.vimarket.zkplus 设置信息是否可见.
     * 
     * @param comp 组件
     * @param attrViewMap 信息是否可见映照
     */
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrConstrain(final Component comp, Map<Object, String> attrConstrainMap) {
        super.setAttrConstrain(comp, attrConstrainMap);
        // 自身不隐藏，取下级数据
        // panel//panelchild
        final Component compPanelchild = comp.getLastChild();
        final List<Component> subHboxList = compPanelchild.getChildren();
        final int sum = subHboxList == null ? 0
            : subHboxList.size();
        AttrPanelComp.logger.debug("subHboxList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final Component subHbox = subHboxList.get(i);
            final List<Component> subCompRootBoxList = subHbox.getChildren();
            final int jsum = subCompRootBoxList == null ? 0
                : subCompRootBoxList.size();
            for (int j = 0; j < jsum; j++) {
                final Component subCompRootBox = subCompRootBoxList.get(j);
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setAttrConstrain(
                        subCompRootBox, attrConstrainMap);
                    System.out.println(AttrCompFactory.getInstByComponent(subCompRootBox)
                        .getClass().getName());
                }
            }
        }
    }
    
}
